//
// Created by 86176 on 2021/11/5.
//

#include "DefaultFilterChain.h"


void DefaultFilterChain::RecvMessage(Session *session) {
    FilterChain * nextfilterchain = head;
    Message *message = NULL;
//    int recvLen = (int)session->ioRecv;
//    memset(session->ioBuffer->byteStream + recvLen,'\0',1);
    head->messageRecevide(head,session,message);
}

DefaultFilterChain::DefaultFilterChain() {
    this->head = new HeadFilterChain("head");
    this->tail = new TailFilterChain("tail");
    head->nextChain = tail;
    head->linkToTail = true;
    tail->lastChain = head;
}

void DefaultFilterChain::addChainLast(FilterChain *chain) {
    FilterChain *filterChain = head;
    while (!filterChain->linkToTail){
        filterChain = filterChain->nextChain;
    }
    filterChain->linkToTail = false;
    chain->nextChain = filterChain->nextChain;
    filterChain->nextChain = chain;
    chain->linkToTail = true;
}

void DefaultFilterChain::setIOhander(IoHandler *ioHandler) {
    TailFilterChain* tempChain = (TailFilterChain*)tail;
//    TailFilterChain* tempChain = dynamic_cast<TailFilterChain*>(tail);
    if (tempChain != NULL)
    {
        tempChain->setIOHander(ioHandler);
    }
}

IoHandler *DefaultFilterChain::getIOhander() {
    TailFilterChain* tempChain = (TailFilterChain*)tail;
    //TailFilterChain* tempChain = dynamic_cast<TailFilterChain*>(tail);
    if (tempChain != NULL)
    {
        return tempChain->getIoHandler();
    }
    return nullptr;
}
